package com.power.sifa.approve.executor.impl;

import com.plf.common.domain.ResponseDTO;
import com.power.flowable.api.ILog_limitscatFeign;
import com.power.sifa.approve.service.Apl_admre_auditService;
import com.power.sifa.util.executor.AbstractPostExecutor;
import com.power.sifa.approve.model.Apl_admre_audit;
import com.power.sifa.approve.model.Apl_admreview;
import com.power.sifa.approve.model.Apl_main;
import com.power.sifa.approve.model.enumeration.Tp_apptype;
import com.power.sifa.approve.model.enumeration.Tp_proctype;
import com.power.sifa.approve.model.enumeration.Tp_proctypeEnum;
import com.power.sifa.approve.service.Apl_admreviewService;
import com.power.sifa.approve.service.Apl_mainService;
import com.power.sifa.approve.service.BusinessApproveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * 程序转换完成服务
 */
@Component
@Slf4j
public class HearAduitTransform  extends AbstractPostExecutor {

    // 用户id
    private String userid;
    // 环节分类标识
    private String category;
    // 用户名
    private String username;
    // 单位id
    private String sectionid;
    // 单位名称
    private String sectionname;
    // 部门id
    private String deptid;
    // 部门名称
    private String deptname;
    // 处理意见
    private String remark;
    // 行政区划
    private String adid;
    // 环节名称
    private String taskname;

    @Resource
    private Apl_admreviewService apl_admreviewService;
    @Resource
    private Apl_mainService apl_mainService;
    @Resource
    private BusinessApproveService businessApproveService;
    @Resource
    private ILog_limitscatFeign iLog_limitscatFeign;
    @Resource
    private Apl_admre_auditService apl_admre_auditService;

    @Override
    public void init(Map<String, Object> initdata) {
        this.userid = (String) initdata.get("userid");
        this.category = (String) initdata.get("category");
        this.username = (String) initdata.get("username");
        this.sectionid = (String) initdata.get("sectionid");
        this.sectionname = (String) initdata.get("sectionname");
        this.deptid = (String) initdata.get("deptid");
        this.deptname = (String) initdata.get("deptname");
        this.remark = (String) initdata.get("remark");
        this.adid = (String) initdata.get("adid");
        this.taskname = (String) initdata.get("taskname");
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> execute() throws Exception {
        // 1. 生成和保存三级审批文书
        Apl_admre_audit auditObj = this.businessApproveService.createThreeLevelApprove(super.applyid, this.userid, this.username, Tp_proctype.HEAR_TRANSFORM);
        Apl_admreview admreObj = this.apl_admreviewService.getById(auditObj.getApplyid());
        // 2. 业务程序类型状态切换
        if(admreObj.getProctype() == null) {
            log.error("HearAduitTransform 主表程序类型为空");
            throw new RuntimeException("主表程序类型为空");
        }
        if(Tp_apptype.EASY == admreObj.getProctype()) {
            admreObj.setProctype(Tp_apptype.ORDINARY);
            this.apl_admreviewService.updateProctype(admreObj.getId(), Tp_apptype.ORDINARY);
        } else if(Tp_apptype.ORDINARY == admreObj.getProctype()) {
            admreObj.setProctype(Tp_apptype.EASY);
            this.apl_admreviewService.updateProctype(admreObj.getId(), Tp_apptype.EASY);
        } else {
            log.error("HearAduitTransform 主表程序类型切换失败");
            throw new RuntimeException("主表程序类型切换失败");
        }

        // 3. 删除一个子状态
        String sonStatus = this.businessApproveService.delSonStatus(admreObj.getSonstatus(), Tp_proctypeEnum.HEAR_TRANSFORM.getStatus());
        this.apl_admreviewService.updatesSonStatus(admreObj.getId(), sonStatus);

        // 刷新文书日期
        this.businessApproveService.processEndedUpdatePreviewDoc(auditObj.getApplyid(), Tp_proctype.HEAR_TRANSFORM, null);
        // 文书盖章
        this.businessApproveService.templateStamp(auditObj.getApplyid(), Tp_proctype.HEAR_TRANSFORM);

        // 转换成功后办结时限
        try {
            // 查询办结时限信息
            Apl_main faMainObj = this.apl_mainService.getById(auditObj.getApplyid());
            // 根据计算期满日期, 查询办结时限信息
            String endDate = this.businessApproveService.computeExpiryDate(admreObj.getProctype(), faMainObj.getAccepttime());
            // 更新办结时限信息
            ResponseDTO updateRes = this.iLog_limitscatFeign.updateById(faMainObj.getInstid(), faMainObj.getScatid(), endDate);
            // 5. 更新业务主表期满日期
            String quedate = new SimpleDateFormat("yyyy年M月d日").format(new SimpleDateFormat("yyyy-MM-dd").parse(endDate));
            this.apl_admreviewService.updateQuedate(faMainObj.getId(), quedate);
            // 5.1 更新业务从表期满日期
            this.apl_admre_auditService.updateQuedate(faMainObj.getId(), quedate);
            if(0 != updateRes.getCode()) {
                throw new RuntimeException(updateRes.getMsg());
            }
        } catch (Exception e) {
            log.error("HearAduitTransform 根据环节组id更新截止日期失败 ->  msg[{}]", e.getMessage());
        }
        return null;
    }

}
